package mysql

import "github.com/jmoiron/sqlx"

// InsertCategory 插入分类
func InsertCategory(category *model.Category) (categoryID int64, err error) {
	sqlstr := "insert into category(category_name, category_no) value(?, ?)"
	result, err := DB.Exec(sqlstr, category.CategoryName, category.CategoryNo)
	if err != nil {
		return
	}
	categoryID, err := result.LastInsertId()
	return
}

// GetCategoryById 根据id获取单个类别
func GetCategoryById(id int64) (category *model.Category, err error) {
	category := &model.Category{}
	sqlstr := "select id,category_name,category_no from category where id = ?"
	err := DB.Get(category, sqlstr, id)
	return
}

// GetCategoryList 根据数组id获取多个类别
func GetCategoryList(categoryIds []int64) (categoryList []*model.Category, err error) {
	sqlstr, args, err := sqlx.In("select id,category_name,category_no from category where id in(?)", categoryIds)
	if err != nil {
		return
	}
	// 查询
	err := DB.select(&categoryList, sqlstr, args...)
	return
}

// GetAllCategoryList 获取所有类别
func GetAllCategoryList() (categoryList []*model.Category, err error) {
	sqlstr := "select id,category_name,category_no from category order by category_no asc"
	// 查询
	err := DB.select(&categoryList, sqlstr.)
	return
}
